#coding: utf-8
from twisted.application import internet, service
from twisted.internet.protocol import Protocol, Factory
from twisted.persisted import sob
class Fibonacci(Protocol):
    "Serve a sequence of Fibonacci numbers to all requesters"
    def dataReceived(self, data):
        self.factory.new = self.factory.a + self.factory.b
        self.transport.write('%d' % self.factory.new)
        self.factory.a = self.factory.b
        self.factory.b = self.factory.new
def main():
    import fib_server    # Use script as namespace
    f = Factory()
    f.protocol = fib_server.Fibonacci
    f.a, f.b = 1, 1
    application = service.Application("Fibonacci")
    serv=internet.TCPServer(8888, f)
    serv.setServiceParent(application)
    sob.IPersistable(application).save()
if'__main__' == __name__:
    main()